package com.socialcoco.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.socialcoco.entity.DiaryRespone;
import com.socialcoco.entity.ILike;
import com.socialcoco.mapper.DiaryResMapper;
import com.socialcoco.respone.BaseResponse;
import com.socialcoco.respone.ResultUtils;
import com.socialcoco.serviceImpl.ILikeServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵志浩
 * @since 2022-03-17 09:16:50
 */

/**
 * 用户点赞的统计和显示
 */
@Slf4j
@RestController
@RequestMapping("/socialcoco/user")
public class ILikeController {
    @Autowired
    ILikeServiceImpl iLikeService;
    @Autowired
    DiaryResMapper diaryResMapper;
    @ApiOperation("用户点赞,uid表示点赞的用户，did表示点赞的动态，flag表示点赞还是取消点赞")
    @PostMapping("/good")
    public BaseResponse<Object> goodD(@RequestHeader("token")String token,@RequestBody ILike like){
        QueryWrapper<ILike> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dzid",like.getDzid()).eq("uid",like.getUid());
        List<ILike> list = iLikeService.list(queryWrapper);
        if(list!=null&&list.size()>0){
            like=list.get(0);
            iLikeService.removeById(like.getZid());
            return ResultUtils.success("okz");
        }
        else{
            like.setCreateTime(new Date());
            iLikeService.save(like);
            return ResultUtils.success(like.getZid());
        }
    }
    /***
     * 显示用户点赞数量前10的动态
     *
     */
    @ApiOperation("用户点赞,uid表示点赞的用户，did表示点赞的动态，flag表示点赞还是取消点赞")
    @GetMapping("/rank")
    public BaseResponse<Object> rank(@RequestHeader("token")String token){
        List<DiaryRespone> diaryRespones = diaryResMapper.rankList();
        return  ResultUtils.success(diaryRespones);
    }

}

